package com.tf.integrated.controller;

import com.ccssoft.m8.common.ccssoft.modules.util.EntityWrapperHandle;
import com.ccssoft.m8.common.web.BaseController;
import com.tf.integrated.entity.*;
import com.tf.integrated.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * 获取结构化数据列表接口
 * @author allen
 * @date: 2020年9月14日上午10:56:20
 */
@Api(value = "获取结构化数据列表接口",description="StructureDataListController")
@RestController
public class StructureDataListController extends BaseController
{
    @Autowired
    private EventresultvehicleinfoService<EventresultvehicleinfoEntity> vehicleService;

    @Autowired
    private EventresultpersonService<EventresultpersonEntity> personService;

    @Autowired
    private EventresulttricyclesinfoService<EventresulttricyclesinfoEntity> tricycleService;

    @Autowired
    private EventresultpedestriansinfoService<EventresultpedestriansinfoEntity> pedestrianService;

    @Autowired
    private EventresultbikesinfoService<EventresultbikesinfoEntity> bikeService;

    @ApiOperation("获取结构化数据列表接口")
    @GetMapping("/structureDataList")
    @CrossOrigin(origins = "*",maxAge = 86400)
    public StructureDataReturnEntity<List<JSONObject>> structureDataList(@RequestParam Map<String, Object> data) {
        if(data!=null&&data.size()>0) {
            String host = (String)data.get("host");
            String chnId = (String)data.get("chnId");
            String spage=(String)data.get("page");
            String ssize=(String)data.get("size");
            int page=0;
            int size=0;
            if(isExist(spage)&&isInteger(spage)){
                page=Integer.parseInt(spage);
            }
            if(isExist(ssize)&&isInteger(ssize)){
                size=Integer.parseInt(ssize);
            }
            int total=0;
            List<JSONObject> all=new ArrayList<>();
            if (isExist(host) && isExist(chnId) && page >= 1 && size > 0 && size <= 200) {
                Integer[] types = getTypes((String)data.get("types"));
                for (int type : types) {
                    switch (type) {
                        case 1: {
                            all.addAll(getListTypeOne(host,chnId));
                            break;
                        }
                        case 2: {
                            all.addAll(getListTypeTwo(host,chnId));
                            break;
                        }
                        case 3: {
                            all.addAll(getListTypeThree(host,chnId));
                            break;
                        }
                        case 4: {
                            all.addAll(getListTypeFour(host,chnId));
                            break;
                        }
                        case 5: {
                            all.addAll(getListTypeFive(host,chnId));
                            break;
                        }
                    }
                }
                total=all.size();
                all.sort((a1,a2)-> a2.getDate("createtime").compareTo(a1.getDate("createtime")));
                int begin = (page - 1) * size;
                int end = begin + size;
                if(begin>=all.size()){
                    all.clear();
                }else{
                    all= all.subList(begin,Math.min(end,all.size()));
                }
            }
            return new StructureDataReturnEntity<List<JSONObject>>().ok(all,200,total);
        }
        return  new StructureDataReturnEntity<List<JSONObject>>().error("size or page err");
    }

    public ArrayList<JSONObject> getListTypeOne(String host,String chn){
        EntityWrapperHandle<EventresultvehicleinfoEntity> ew = new EntityWrapperHandle<EventresultvehicleinfoEntity>();
        ew.eq("chn",chn);
        ew.eq("deviceid",host);
        List<EventresultvehicleinfoEntity> list = vehicleService.selectList(ew);
        ArrayList<JSONObject> array=new ArrayList<>();
        if(list!=null&&list.size()>0){
            for (EventresultvehicleinfoEntity eventresultvehicleinfoEntity : list) {
                JSONObject json = new JSONObject();
                JSONObject se = new JSONObject();
                json.put("id", eventresultvehicleinfoEntity.getId());
                json.put("CarType", eventresultvehicleinfoEntity.getCartype());
                json.put("Chn", eventresultvehicleinfoEntity.getChn());
                json.put("CoDriver", eventresultvehicleinfoEntity.getCodriver());
                json.put("CoNoBelt", eventresultvehicleinfoEntity.getConobelt());
                json.put("Color", eventresultvehicleinfoEntity.getColor());
                json.put("DriveWay", eventresultvehicleinfoEntity.getDriveway());
                json.put("Drop", eventresultvehicleinfoEntity.getDrop());
                json.put("FullPicPath", eventresultvehicleinfoEntity.getFullpicpath());
                json.put("HasCall", eventresultvehicleinfoEntity.getHascall());
                json.put("HasCrash", eventresultvehicleinfoEntity.getHascrash());
                json.put("HasRack", eventresultvehicleinfoEntity.getHasrack());
                json.put("HasSpareTire", eventresultvehicleinfoEntity.getHassparetire());
                json.put("HasSunroof", eventresultvehicleinfoEntity.getHassunroof());
                json.put("IsDanger", eventresultvehicleinfoEntity.getIsdanger());
                json.put("IsSlag", eventresultvehicleinfoEntity.getIsslag());
                json.put("MainNoBelt", eventresultvehicleinfoEntity.getMainnobelt());
                json.put("MarkerNum", eventresultvehicleinfoEntity.getMarkernum());
                json.put("ObjPicPath", eventresultvehicleinfoEntity.getObjpicpath());
                json.put("Orientation", eventresultvehicleinfoEntity.getOrientation());
                json.put("Paper", eventresultvehicleinfoEntity.getPaper());
                json.put("PlateInfo", eventresultvehicleinfoEntity.getPlateinfo());
                json.put("Rect", eventresultvehicleinfoEntity.getRect());
                json.put("StyleName", eventresultvehicleinfoEntity.getStylename());
                json.put("Time", eventresultvehicleinfoEntity.getTime());
                json.put("Type", eventresultvehicleinfoEntity.getType());
                se.put("resultType", 1);
                se.put("info", json);
                se.put("createtime", eventresultvehicleinfoEntity.getCreatetime());
                array.add(se);
            }
        }
        return array;
    }

    public ArrayList<JSONObject> getListTypeTwo(String host,String chn){
        EntityWrapperHandle<EventresulttricyclesinfoEntity> ew = new EntityWrapperHandle<EventresulttricyclesinfoEntity>();
        ew.eq("chn",chn);
        ew.eq("deviceid",host);
        List<EventresulttricyclesinfoEntity> list = tricycleService.selectList(ew);
        ArrayList<JSONObject> array=new ArrayList<>();
        if(list!=null&&list.size()>0) {
            for (EventresulttricyclesinfoEntity eventresulttricyclesinfoEntity : list) {
                JSONObject json = new JSONObject();
                JSONObject se = new JSONObject();
                json.put("id", eventresulttricyclesinfoEntity.getId());
                json.put("Chn", eventresulttricyclesinfoEntity.getChn());
                json.put("Convertible", eventresulttricyclesinfoEntity.getConvertible());
                json.put("DriveWay", eventresulttricyclesinfoEntity.getDriveway());
                json.put("FullPicPath", eventresulttricyclesinfoEntity.getFullpicpath());
                json.put("Manned", eventresulttricyclesinfoEntity.getManned());
                json.put("ObjPicPath", eventresulttricyclesinfoEntity.getObjpicpath());
                json.put("Rect", eventresulttricyclesinfoEntity.getRect());
                json.put("RoofColor", eventresulttricyclesinfoEntity.getRoofcolor());
                json.put("Time", eventresulttricyclesinfoEntity.getTime());
                json.put("Type", eventresulttricyclesinfoEntity.getType());
                se.put("resultType", 2);
                se.put("info", json);
                se.put("createtime", eventresulttricyclesinfoEntity.getCreatetime());
                array.add(se);
            }
        }
        return array;
    }

    public ArrayList<JSONObject> getListTypeThree(String host,String chn){
        EntityWrapperHandle<EventresultpedestriansinfoEntity> ew = new EntityWrapperHandle<EventresultpedestriansinfoEntity>();
        ew.eq("chn",chn);
        ew.eq("deviceid",host);
        List<EventresultpedestriansinfoEntity> list = pedestrianService.selectList(ew);
        ArrayList<JSONObject> array=new ArrayList<>();
        if(list!=null&&list.size()>0) {
            for (EventresultpedestriansinfoEntity eventresultpedestriansinfoEntity : list) {
                JSONObject json = new JSONObject();
                JSONObject se = new JSONObject();
                json.put("id", eventresultpedestriansinfoEntity.getId());
                json.put("Age", eventresultpedestriansinfoEntity.getAge());
                json.put("Baby", eventresultpedestriansinfoEntity.getBaby());
                json.put("Bag", eventresultpedestriansinfoEntity.getBag());
                json.put("BottomColor", eventresultpedestriansinfoEntity.getBottomcolor());
                json.put("BottomType", eventresultpedestriansinfoEntity.getBottomtype());
                json.put("Chn", eventresultpedestriansinfoEntity.getChn());
                json.put("DriveWay", eventresultpedestriansinfoEntity.getDriveway());
                json.put("FullPicPath", eventresultpedestriansinfoEntity.getFullpicpath());
                json.put("Glasses", eventresultpedestriansinfoEntity.getGlasses());
                json.put("Hair", eventresultpedestriansinfoEntity.getHair());
                json.put("Hat", eventresultpedestriansinfoEntity.getHat());
                json.put("Knapsack", eventresultpedestriansinfoEntity.getKnapsack());
                json.put("Mask", eventresultpedestriansinfoEntity.getMask());
                json.put("MessengerBag", eventresultpedestriansinfoEntity.getMessengerbag());
                json.put("ObjPicPath", eventresultpedestriansinfoEntity.getObjpicpath());
                json.put("Orientation", eventresultpedestriansinfoEntity.getOrientation());
                json.put("Rect", eventresultpedestriansinfoEntity.getRect());
                json.put("Sex", eventresultpedestriansinfoEntity.getSex());
                json.put("ShoulderBag", eventresultpedestriansinfoEntity.getShoulderaag());
                json.put("Time", eventresultpedestriansinfoEntity.getTime());
                json.put("Type", eventresultpedestriansinfoEntity.getType());
                json.put("Umbrella", eventresultpedestriansinfoEntity.getUmbrella());
                json.put("UpperColor", eventresultpedestriansinfoEntity.getUppercolor());
                json.put("UpperTexture", eventresultpedestriansinfoEntity.getUppertexture());
                json.put("UpperType", eventresultpedestriansinfoEntity.getUppertype());
                se.put("resultType", 3);
                se.put("info", json);
                se.put("createtime", eventresultpedestriansinfoEntity.getCreatetime());
                array.add(se);
            }
        }
        return array;
    }

    public ArrayList<JSONObject> getListTypeFour(String host,String chn){
        EntityWrapperHandle<EventresultbikesinfoEntity> ew = new EntityWrapperHandle<EventresultbikesinfoEntity>();
        ew.eq("chn",chn);
        ew.eq("deviceid",host);
        List<EventresultbikesinfoEntity> list = bikeService.selectList(ew);
        ArrayList<JSONObject> array=new ArrayList<>();
        if(list!=null&&list.size()>0) {
            for (EventresultbikesinfoEntity eventresultbikesinfoEntity : list) {
                JSONObject se = new JSONObject();
                JSONObject json = new JSONObject();
                json.put("id", eventresultbikesinfoEntity.getId());
                json.put("Age", eventresultbikesinfoEntity.getAge());
                json.put("Baby", eventresultbikesinfoEntity.getBaby());
                json.put("Bag", eventresultbikesinfoEntity.getBag());
                json.put("BottomColor", eventresultbikesinfoEntity.getBottomcolor());
                json.put("BottomType", eventresultbikesinfoEntity.getBottomtype());
                json.put("Chn", eventresultbikesinfoEntity.getChn());
                json.put("DriveWay", eventresultbikesinfoEntity.getDriveway());
                json.put("FullPicPath", eventresultbikesinfoEntity.getFullpicpath());
                json.put("Glasses", eventresultbikesinfoEntity.getGlasses());
                json.put("Hair", eventresultbikesinfoEntity.getHair());
                json.put("Hat", eventresultbikesinfoEntity.getHat());
                json.put("Knapsack", eventresultbikesinfoEntity.getKnapsack());
                json.put("Mask", eventresultbikesinfoEntity.getMask());
                json.put("MessengerBag", eventresultbikesinfoEntity.getMessengerbag());
                json.put("ObjPicPath", eventresultbikesinfoEntity.getObjpicpath());
                json.put("Orientation", eventresultbikesinfoEntity.getOrientation());
                json.put("Rect", eventresultbikesinfoEntity.getRect());
                json.put("Sex", eventresultbikesinfoEntity.getSex());
                json.put("ShoulderBag", eventresultbikesinfoEntity.getShoulderaag());
                json.put("Time", eventresultbikesinfoEntity.getTime());
                json.put("Type", eventresultbikesinfoEntity.getType());
                json.put("Umbrella", eventresultbikesinfoEntity.getUmbrella());
                json.put("UpperColor", eventresultbikesinfoEntity.getUppercolor());
                json.put("UpperTexture", eventresultbikesinfoEntity.getUppertexture());
                json.put("UpperType", eventresultbikesinfoEntity.getUppertype());
                se.put("resultType", 4);
                se.put("info", json);
                se.put("createtime", eventresultbikesinfoEntity.getCreatetime());
                array.add(se);
            }
        }
        return array;
    }

    public ArrayList<JSONObject> getListTypeFive(String host,String chn){
        EntityWrapperHandle<EventresultpersonEntity> ew = new EntityWrapperHandle<EventresultpersonEntity>();
        ew.eq("chn",chn);
        ew.eq("deviceid",host);
        List<EventresultpersonEntity> list = personService.selectList(ew);
        ArrayList<JSONObject> array=new ArrayList<>();
        if(list!=null&&list.size()>0) {
            for (EventresultpersonEntity eventresultpersonEntity : list) {
                JSONObject json = new JSONObject();
                JSONObject se = new JSONObject();
                json.put("id", eventresultpersonEntity.getId());
                json.put("Age", eventresultpersonEntity.getAge());
                json.put("Chn", eventresultpersonEntity.getChn());
                json.put("FullPicPath", eventresultpersonEntity.getFullpicpath());
                json.put("Glasses", eventresultpersonEntity.getGlasses());
                json.put("Hair", eventresultpersonEntity.getHair());
                json.put("Hat", eventresultpersonEntity.getHat());
                json.put("Mask", eventresultpersonEntity.getMask());
                json.put("ObjPicPath", eventresultpersonEntity.getObjpicpath());
                json.put("Rect", eventresultpersonEntity.getRect());
                json.put("RegionName", eventresultpersonEntity.getRegionname());
                json.put("Sex", eventresultpersonEntity.getSex());
                json.put("Time", eventresultpersonEntity.getTime());
                json.put("Type", eventresultpersonEntity.getType());
                se.put("resultType", 5);
                se.put("info", json);
                se.put("createtime", eventresultpersonEntity.getCreatetime());
                array.add(se);
            }
        }
        return array;
    }

    public boolean isExist(String value){
        return value != null && !value.equals("") ;
    }

    public boolean isInteger(String str) {
        Pattern pattern = Pattern.compile("^[-+]?[\\d]*$");
        return pattern.matcher(str).matches();
    }

    public Integer[] getTypes(String types){
        if(!isExist(types)){
            return new Integer[]{1,2,3,4,5};
        }
        String[] str = types.split(",");
        List<Integer> list = new ArrayList<>();
        for (String value : str) {
            if (isInteger(value)) {
                list.add(Integer.parseInt(value));
            }
        }
        return list.toArray(new Integer[0]);
    }
}